package gae.colopl;

import java.util.ArrayList;
import java.util.List;

import gae.colopl.bean.User;
import gae.colopl.bean.WareHouseItem;
import gae.colopl.http.ColoplLoginException;
import gae.colopl.http.ColoplRequestException;
import gae.colopl.http.ColoplRequestManager;
import gae.colopl.http.bean.ItemWHData;
import gae.colopl.http.bean.ItemWHItemData;
import gae.colopl.http.user.Session;

public class ColoplServiceImplOld {
    
    private static ColoplServiceImplOld service = new ColoplServiceImplOld();
    
    private static ColoplRequestManager manager = ColoplRequestManager.getInstance();
    
    private ColoplServiceImplOld() {}
    
    public static ColoplServiceImplOld getInstance() {
        return service;
    }
    
    public User getUserInfo(String username, String password) throws ColoplLoginException, ColoplRequestException {
        
        Session userSession = manager.requestLogin(username, password);
        
        User userInfo = new User();
        userInfo.setUsername(username);
        userInfo.setPassword(password);
        userInfo.setId(userSession.getId());
        userInfo.setUid(userSession.getUid());
        
        return userInfo;
    }
    
    public List<WareHouseItem> searchFromItemWH(String username, String password, String key) throws ColoplRequestException {
        List<WareHouseItem> result = new ArrayList<WareHouseItem>();
        
        ItemWHData wh = manager.requestItemWH(username, password, 0);
        int pageSize = Integer.valueOf(wh.getPageCount());
        for (int i = 0; i < pageSize; i++) {
            if (i != 0) {
                wh = manager.requestItemWH(username, password, i);
            }
            List<ItemWHItemData> list = wh.getItems();
            for (int j = 0; j < list.size(); j++) {
                ItemWHItemData itemNominee = list.get(j);
                if(itemNominee.getName().contains(key)) {
                    WareHouseItem item = new WareHouseItem();
                    item.setName(itemNominee.getName());
                    String price = itemNominee.getPrice();
                    if (price != null) {
                        item.setPrice(Integer.valueOf(price));
                    }
                    item.setComment(itemNominee.getDescription());
                    item.setPageNumber(i + 1);
                    item.setLineNumber(j + 1);
                    result.add(item);
                }
            }
        }
        
        return result;
    }
    
//    public List<TreasureItem> searchFromTreasureBox(String username, String password, String id, String key) throws ColoplRequestException {
//        
//        
//        return null;
//    }
}
